package com.lazyboyl.sharding.proxy.net.proto.mysql;

import com.lazyboyl.sharding.proxy.net.proto.MySQLPacket;
import com.lazyboyl.sharding.proxy.net.proto.util.BufferUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author linzef
 * @since 2021-06-29
 * 类描述：二进制的接包类
 */
public class BinaryPacket extends MySQLPacket {

    public byte[] data;

    @Override
    public int calcPacketSize() {
        return data == null ? 0 : data.length;
    }

    @Override
    public void write(ChannelHandlerContext ctx) {
        ByteBuf byteBuf = ctx.alloc().buffer();
        BufferUtil.writeUB3(byteBuf, packetLength);
        byteBuf.writeByte(packetId);
        byteBuf.writeBytes(data);
        ctx.writeAndFlush(byteBuf);
    }

    @Override
    protected String getPacketInfo() {
        return "MySQL Binary Packet";
    }
}
